package net.diaowen.common.base.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.diaowen.common.base.entity.User;
import net.diaowen.common.dao.BaseDaoImpl;
import net.diaowen.common.plugs.page.Page;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.springframework.stereotype.Repository;


/**
 * @author keyuan(keyuan258@gmail.com)
 *
 * https://github.com/wkeyuan/DWSurvey
 * http://dwsurvey.net
 */
@Repository
public class UserDaoImpl extends BaseDaoImpl<User, String> implements UserDao{

	@Override
	public void resetUserGroup(String groupId) {
		String sql="UPDATE t_user SET user_group_id = '' WHERE id = id";
		this.getSession().createSQLQuery(sql).executeUpdate();
	}

	@Override
	public List<User> selectorgid(String id) throws Exception {
		try{
			String sql="select name,email from t_user where org_id=?";
			List<Object[]> list=this.getSession().createSQLQuery(sql).setString(0, id).list();
			List<User> users=new ArrayList<User>();
			for (Object[] user:list){
				User user1=new User();
				user1.setName(user[0].toString());
				if(user[1]!=null){
					user1.setEmail(user[1].toString());
				}
				users.add(user1);
				System.out.println("输出user的name,user的email"+user[0]+user[1]);
			}
			return users;
		}
		catch (Exception e){
			e.printStackTrace();
		}
		return new ArrayList<User>();
	}

	@Override
	public int createuser(String orgid, String name, String email) {
		//未实现
		try{
			String sql="insert t_user ";
			this.getSession().createSQLQuery(sql).executeUpdate();
		}
		catch (Exception e){
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public int deleteuser(String email, String name) throws Exception{
		try{
			String sql="delete from t_user where email = ? and name = ?";
			Query query=this.getSession().createSQLQuery(sql);
			query.setParameter(0,email);
			query.setParameter(1,name);
			int result=query.executeUpdate();
			return result;
		}
		catch (Exception e){
			int result = 0;
			e.printStackTrace();
		}
		return 0;
	}

}
